﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
using Jumbot.Common;
using JumbotCms.API;

namespace JumbotCms.WebFile.Admin.Cut2Thumb
{
    public partial class _upfile : AdminCenter
    {
        private string _sUserUploadPath;
        private string _sUserUploadType;
        private int _sUserUploadSize = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            ChannelId = Str2Str(q("ccid"));
            Admin_Load("", "json", true);
            this._sUserUploadPath = site.Dir + "_data/tempfiles";
            this._sUserUploadType = ".jpg.jpeg.bmp.";
            this._sUserUploadSize = 2048;
            if (Request.Files.Count > 0)
            {
                HttpPostedFile oFile = Request.Files[0];//得到要上传文件
                if (oFile != null && oFile.ContentLength > 0)
                {
                    try
                    {
                        string fileContentType = oFile.ContentType; //文件类型
                        string fileExtension = System.IO.Path.GetExtension(oFile.FileName).ToLower(); //上传文件的扩展名
                        string F_Type = fileExtension.Substring(1, fileExtension.Length - 1);
                        //if (this._sUserUploadType.ToLower().IndexOf("." + F_Type + ".") > -1)//检测是否为允许的上传文件类型
                        if (this._sUserUploadType.ToLower().Contains("." + F_Type + "."))
                        {
                            if (this._sUserUploadSize * 1024 >= oFile.ContentLength)//检测文件大小是否超过限制
                            {
                                string DirectoryPath;
                                DirectoryPath = this._sUserUploadPath + "/Master_" + AdminId;
                                Jumbot.Common.DirFile.CreateDir(this._sUserUploadPath + "/Master_" + AdminId);

                                string sFileName = "Temp" + fileExtension;  // 文件名称
                                string FullPath = DirectoryPath + "/" + sFileName;        // 服务器端文件路径
                                oFile.SaveAs(Server.MapPath(FullPath));
                                string[] toWidthHeight = q("ThumbSize").Split('|');
                                string toWidth = toWidthHeight[0];
                                string toHeight = toWidthHeight[1];
                                string cutType = q("CutType");
                                System.Drawing.Image originalImage = System.Drawing.Image.FromFile(Server.MapPath(FullPath));
                                if (originalImage.Width > 2048 || originalImage.Height > 1536)
                                {
                                    Response.Write(JsonResult(0, "原图片尺寸太大"));
                                    originalImage.Dispose();
                                }
                                else if (originalImage.Width < Convert.ToInt32(toWidth) || originalImage.Height < Convert.ToInt32(toHeight))
                                {
                                    Response.Write(JsonResult(0, "原图片尺寸小于缩略图尺寸"));
                                    originalImage.Dispose();
                                }
                                else
                                {
                                    originalImage.Dispose();
                                    Response.Write(JsonResult(1, "index.aspx?ccid=" + ChannelId + "&tphoto=" + FullPath + "&tow=" + toWidth + "&toh=" + toHeight + "&type=" + cutType + "&i=1"));
                                }
                            }
                            else//文件大小超过限制
                            {
                                Response.Write(JsonResult(0, "图片大小" + Convert.ToInt32(oFile.ContentLength / 1024) + "KB,超出限制。"));
                            }
                        }
                        else //文件类型不允许上传
                        {
                            Response.Write(JsonResult(0, "上传的不是图片。"));
                        }
                    }
                    catch
                    {
                        Response.Write(JsonResult(0, "程序异常，上传未成功。"));
                    }
                }
                else
                {
                    Response.Write(JsonResult(0, "请选择上传文件"));
                }
            }
            else
                Response.Write(JsonResult(0, "上传有误"));
        }
    }
}
